package com.lizhi.yun.core.organ.handler;

import com.lizhi.common.yun.entity.ListResponseResult;
import com.lizhi.common.yun.enums.OrganStateEnum;
import com.lizhi.yun.db.dao.UppOrganDao;
import com.lizhi.yun.db.pojo.UppOrganPOJO;
import com.lizhi.mybatis.interceptor.PageBounds;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.date.DateUtils;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.base.BaseHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UppOrganHandle extends BaseHandler {

    @Resource
    private UppOrganDao uppOrganDao;

    public ListResponseResult doMain(Map<String, Object> map) {
        PageBounds pageBounds = getPageBounds(map);
        List<UppOrganPOJO> listUppOrganDO = uppOrganDao.selectByMap(map, pageBounds);
        return getListResult(pageBounds.getCountRows(), listUppOrganDO, map);
    }

    /**
     *功能描述  根据机构id查询
     * @author lizhi
     * @date 2020/4/14
     * @param organCd:
     * @return com.lizhi.db.upp.pojo.UppOrganPOJO
     */
    public UppOrganPOJO findById(String organCd) {
        UppOrganPOJO uppOrganPOJO = uppOrganDao.selectById(organCd);
        if (uppOrganPOJO == null) {
            return null;
        }
        return uppOrganPOJO;
    }

    public ListResponseResult insert(UppOrganPOJO uppOrganPOJO) {
        uppOrganPOJO.setOrganId(StringUtils.newId());
        uppOrganPOJO.setIstTm(DateUtils.getNowDate());
        uppOrganPOJO.setState(OrganStateEnum.NORMAL.getEnValue());
        return getListResult(uppOrganDao.insertRecord(uppOrganPOJO));
    }

    public ListResponseResult update(UppOrganPOJO uppOrganPOJO) {
        uppOrganPOJO.setUpdTm(DateUtils.getNowDate());
        return getListResult(uppOrganDao.updateByIdSelective(uppOrganPOJO));
    }

    public ListResponseResult delete(String organId) {
        return getListResult(uppOrganDao.deleteById(organId));
    }

    public boolean isNotexists(String organId) {
        UppOrganPOJO uppOrganPOJO = uppOrganDao.selectById(organId);
        if (null == uppOrganPOJO) {
            return true;
        }
        return false;
    }
    
	public String selectOrgan(String organId, String organNm){
		List<Map<String,Object>> listOrganDO = uppOrganDao.selectOrgan(organId, organNm, OrganStateEnum.NORMAL.getEnValue());
		String json = "{\"message\": \"\",\"value\": "+ SamJsonUtil.toJson(listOrganDO)+",\"code\": 200,\"redirect\": \"\"}";
		return json;
	}

	/**
	 *功能描述   根据机构id获取机构名称
	 * @author lizhi
	 * @date 2020/4/14
	 * @param organId:
	 * @return java.lang.String
	 */
	public String findOrganNmById(String organId){
        UppOrganPOJO uppOrganPOJO = uppOrganDao.selectById(organId);
        if (null == uppOrganPOJO) {
            return "";
        }
        return uppOrganPOJO.getOrganNm();
    }

}